<template>
  <form action="/">
    <van-search
        v-model="searchText"
        show-action
        placeholder="请输入搜索标签"
        @search="onSearch"
        @cancel="onCancel"
    />
    <!--    已选标签-->
    <van-divider content-position="left">已选标签</van-divider>
    <div v-if="activeId.length===0">暂无选择标签</div>
    <van-row gutter="20">
      <van-col v-for="tag in activeId">
        <van-tag closeable size="large" type="primary" @close="doClose(tag)">
          {{tag}}
        </van-tag>
      </van-col>
    </van-row>


    <!--    可选标签-->
    <van-divider content-position="left">选择标签</van-divider>
    <van-tree-select
        v-model:active-id="activeId"
        v-model:main-active-index="activeIndex"
        :items="tagList"
    />
    <div style="padding: 12px">
      <van-button block icon-position="right" type="primary" @click="toSearchResult">查询</van-button>
    </div>
  </form>

</template>

<script setup>
import { ref } from 'vue';
import {useRouter} from "vue-router";
const router = useRouter()
const searchText = ref('');

//已选中的标签
const activeId = ref([]);
const activeIndex = ref(0);
//标签列表
const originalList = [
  {
    text: '性别',
    children: [
      { text: '男', id: '男' },
      { text: '女', id: '女' },
    ],
  },
  {
    text: '学历',
    children: [
      { text: '初中', id: '初中' },
      { text: '高中', id: '高中' },
      { text: '大1', id: '大1' },
      { text: '大2', id: '大2' },
      { text: '大3', id: '大3' },
      { text: '大4', id: '大4' },
      { text: '研1', id: '研1' },
      { text: '研2', id: '研2' },
      { text: '研3', id: '研3' },
    ],
  },
  {
    text: '目标',
    children: [
      { text: '校招', id: '校招' },
      { text: '社招', id: '社招' },
      { text: '春招', id: '春招' },
      { text: '秋招', id: '秋招' }
    ],
  },
  {
    text: '语言',
    children: [
      { text: 'java', id: 'java' },
      { text: 'python', id: 'python' },
      { text: 'go', id: 'go' },
      { text: 'c++', id: 'c++' }
    ],
  },
];

const tagList=ref(originalList)

//移除标签
const doClose=(tag)=>{
  activeId.value=activeId.value.filter(item=> item!==tag)
}

//搜索过滤
// const onSearch = () => {
//   tagList.value=originalList.map(parentTag=>{
//     const tempChildren=[...parentTag.children];
//     const temParentTag={...parentTag};
//     temParentTag.children=tempChildren.filter(item=>item.text.includes(searchText.value));
//     return temParentTag;
//   })
// }

//搜索过滤
const onSearch = () => {
  // 过滤标签
  const filteredList = originalList.map(parentTag=>{
    const tempChildren=[...parentTag.children];
    const temParentTag={...parentTag};
    temParentTag.children=tempChildren.filter(item=>item.text.includes(searchText.value));
    return temParentTag;
  });

  tagList.value = filteredList;

  // 找到第一个有匹配子标签的父分类并激活它
  const firstMatchIndex = filteredList.findIndex(parent => parent.children.length > 0);
  if (firstMatchIndex !== -1) {
    activeIndex.value = firstMatchIndex;
  }
}

//取消搜索
const onCancel = () =>{
  searchText.value=''
  //恢复标签列表
  tagList.value=originalList
}

//根据标签查询，跳转到查询结果页
const toSearchResult=()=>{
  router.push({
        path:'/user/list',
        query: {
          tags:activeId.value
        }
      }
  )
}

</script>

<style scoped>

</style>